<template>
  <div class="date">
    <span>选择日期 ：</span> 
    <el-date-picker
      v-model="date.start"
      :picker-options='dateFormat'
      style="width:15%"
      @change = '(val)=>date.start = val'
      format='yyyy-MM-dd'
      placeholder="请选择">
    </el-date-picker>  
  
    <el-date-picker
      v-model="date.end"
      :picker-options='dateFormat'
      style="width:15%;margin-left:15px"
      @change = '(val)=>date.end = val'
      format='yyyy-MM-dd'
      placeholder="请选择">
    </el-date-picker>  

    <el-button @click.stop='getData' style='margin-left :30px' type='primary'>统计</el-button>
    <el-button @click.stop='donwLoad' type='success'>导出EXCEL</el-button>
  </div>
</template>

<script>
import { Message } from 'element-ui'
import Bus from './Bus'
export default {
	data () {
		return {
			date: {
				start: new Date(),
				end: new Date(),
			},
			tableData: [],
			dateFormat: {
				// disabledDate (time) {
				// 	return time.getTime() >= new Date().getTime()
				// }
			}
		}
	},

	mounted () {
		setTimeout(() => {
			this.transFormDate()
		}, 100)
	},

	methods: {
		transFormDate () {
			if (typeof this.date.start === 'object') {
				const month = new Date().getMonth() + 1 > 10 ? new Date().getMonth() : '0' + new Date().getMonth()
				this.date.start = `${new Date().getFullYear()}-${month}-${new Date().getDate()}`
			}
			if (typeof this.date.end === 'object') {
				const month = new Date().getMonth() + 1 > 10 ? new Date().getMonth() : '0' + (new Date().getMonth() + 1)
				this.date.end = `${new Date().getFullYear()}-${month}-${new Date().getDate()}`
			}



		},

		getData () {

			if (!this.date.start || !this.date.end) {
				Message.warning('请输入起止日期')
				return
			}

			const params = {
				beginDate: this.date.start,
				endDate: this.date.end
			}

			Bus.$emit('date', params)

			this.Axios.Post(this.URL.getSalaryMonitoringData.url, params)
				.then((res) => {
					const { datas } = res.data
					this.tableData = datas
					this.$emit('table', datas)
				})

		},

		donwLoad () {

			if (!this.tableData.length) {
				Message.warning('暂无数据，请重新搜索')
				return
			}
			const params = {
				beginDate: this.date.start,
				endDate: this.date.end
			}

			window.location.href = this.Axios.baseURI + this.URL.exportSalaryMonitoringExcel.url + '?endDate=' + params.endDate + '&beginDate=' + params.beginDate + '&token=' + localStorage.getItem('token')

		}

	}
}
</script>

<style scoped lang='less'>
.date {
  padding: 20px 0;
  & > span {
    margin-right: 10px;
  }
}
</style>
